Page({
  /**
   * 页面的初始数据
   */
  data: {
    isSelect: true,
    name: "",
    phone: "",
    code: "",
    pwd: "",
    repwd: "",
    countDown: 60,
    captchaShow: false, //显示倒计时
  },
  changeSelect() {
    this.setData({
      isSelect: !this.data.isSelect
    })
  },
  /**
   * 查看协议
   */
  goAgreement() {
    wx.navigateTo({
      url: '/pages/userAgreement/userAgreement'
    })
  },
  /**
   * 用户名赋值
   */
  changeName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  /**
   * 校验用户名的合法性
   */
  checkName() {
    let self = this;
    let tip = "";
    wx.showNavigationBarLoading()
    getApp().network.GET({
      url: "/user.php",
      params: {
        username: this.data.name,
        act: "is_registered"
      },
      success: function (res) {
        console.log("######",res)
        if (res.data) {
          tip = "";
        } else {
          tip = "用户名重复，请重新输入用户名！";
        }
        if (tip) {
          wx.showToast({
            title: tip,
            icon: 'none',
            duration: 2000
          })
        }
        wx.hideNavigationBarLoading()
      },
      fail: function () {
        //失败后

      },
    })
  },
  /**
   * 手机号赋值
   */
  changePhone(e) {
    const value = ("" + e.detail.value).substr(0, 11)
    this.setData({
      phone: value
    })
  },
  /**
   * 启动倒计时
   */
  count() {
    this.interval = setInterval(() => {
      if (this.data.countDown === 0) {
        clearInterval(this.interval)
        this.setData({
          captchaShow: false,
          countDown: 60,
        })
      } else {
        this.setData({
          captchaShow: true,
          countDown: this.data.countDown - 1,
        })
      }
    }, 1000)
  },
  /**
   * 获取验证码
   */
  getCode() {
    const self = this;
    if (!this.data.phone) {
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 2000
      })
      return
    } else if (this.data.phone.length !== 11) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showNavigationBarLoading()
    getApp().network.POST({
        url: "/ajax.php?act=mobile_exist",
        params: {
          mobile: this.data.phone
        },
        success: (res) => {
          if (res.data == 1) {
            wx.showToast({
                title: "该手机号码已经注册",
                icon: 'none',
                duration: 2000
            })
          } else {
            getApp().network.POST({
                url: "/new/send_code",
                params: {
                  phone: this.data.phone,
                  type: "reg"
                },
                success: (res) => {
                  if (res.data.code === 0) {
                    self.count();
                  } else {
                    wx.showToast({
                      title: res.data.explain,
                      icon: 'error',
                      duration: 2000
                    })
                  }
                  wx.hideNavigationBarLoading()
                },
                fail: ()=> {
                  //失败后
          
                },
            })
          }
          wx.hideNavigationBarLoading()
        },
        fail: () => {
          //失败后
  
        },
    })
    
  },
  /**
   * 验证码赋值
   */
  changeCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  /**
   * @description: 检验密码是否符合格式
   * @param {type} pwd{String}9
   * @return {type} Number
   */
  checkPassword(pwd) {
    let verifiers = /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S{6,16}$/
    if (pwd.length < 6 || pwd.length > 16) {
      return 1
    } else if (pwd.match(verifiers) === null) {
      return 2
    } else {
      return 0
    }
  },
  /**
   * 密码赋值
   */
  changePassword(e) {
    let check = this.checkPassword(e.detail.value)
    if (check == 1) {
      wx.showToast({
        title: "输入密码长度需在6-16位之间！",
        icon: 'none',
        duration: 2000
      })
    } else if (check == 2) {
      wx.showToast({
        title: "密码必须包含大写字母、小写字母、数字、-_中的两种及以上！",
        icon: 'none',
        duration: 2000
      })
    }
    this.setData({
      pwd: e.detail.value
    })
  },
  /**
   * 确认密码赋值
   */
  changerepwd(e) {
    if (e.detail.value && this.data.repwd && (e.detail.value !== this.data.pwd)) {
      this.setData({
        different: true
      })
    } else {
      this.setData({
        different: false
      })
    }
    this.setData({
      repwd: e.detail.value
    })
  },
  /**
   * 注册接口
   */
  login() {
    const self = this;
    let check = this.checkPassword(this.data.pwd)
    if (!this.data.name) {
      wx.showToast({
        title: "请输入用户名/公司简称",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.data.phone) {
      wx.showToast({
        title: "请输入手机号码",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.data.code) {
      wx.showToast({
        title: "请输入验证码",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.data.pwd) {
      wx.showToast({
        title: "请输入登录密码",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (check == 1) {
      wx.showToast({
        title: "输入密码长度需在6-16位之间！",
        icon: 'none',
        duration: 2000
      })
      return
    } else if (check == 2) {
      wx.showToast({
        title: "密码必须包含大写字母、小写字母、数字、-_中的两种及以上！",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.data.repwd) {
      wx.showToast({
        title: "请确认登录密码",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.repwd !== this.data.pwd) {
      wx.showToast({
        title: "请保持两次密码一致",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.data.isSelect) {
      wx.showToast({
        title: "请同意并勾选平台协议",
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showNavigationBarLoading()
    getApp().network.POST({
      url: "/new/user_register",
      params: {
        user_name: this.data.name,
        phone: this.data.phone,
        sms_code: this.data.code,
        password: this.data.pwd,
        re_password: this.data.repwd
      },
      noNeedCookie: true,
      noNeedToken: true,
      success: function (res) {
          console.log(res)
        if (res.data.code == 0) {
          wx.showModal({
              title: '注册成功',
              content: '即将跳转登录页面',
              showCancel: false,
              success (res) {
                  if (res.confirm) {
                      wx.redirectTo({
                          url: "/pages/login/login"
                      })
                  }
              }
          })
        } else {
          wx.showModal({
            title: '',
            content: res.data.msg,
            showCancel: false
          })
        }
        wx.hideNavigationBarLoading()
      },
      fail: function () {
        //失败后
      },
    }, self)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
})